package com.apes.framework.plugin.fsm;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.outbox.annotation.OutBoxExclude;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Objects;

/**
 * 功能：状态流
 *
 * @author xul
 * @create 2020-08-27 11:37
 */
@OutBoxExclude
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FsmTask.CONST_TABLE_NAME)
public class FsmTask extends BaseModel {
    public static final String CONST_TABLE_NAME = "fsm_task";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "状态流：任务")
    @Comment("编码")
    private String id;

    @ManyToOne
    @JoinColumn(name = "state", referencedColumnName = "id", nullable = false)
    @Comment("状态")
    private FsmState state;

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_type", referencedColumnName = "id")
    @Comment("任务类型")
    private Enumeration taskType;

    @Column(name = "time_expr", length = 250)
    @Comment("时间表达式")
    private String timeExpr;

    @Column(name = "user_expr", length = 250)
    @Comment("用户表达式")
    private String userExpr;

    @Column(name = "action_expr", length = 250)
    @Comment("动作表达式")
    private String actionExpr;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("是否有效")
    private boolean valid = true;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FsmTask task = (FsmTask) o;
        return Objects.equals(id, task.id);
    }


}
